package com.deer.wms.ware.task.model.SO;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.deer.wms.intercept.annotation.NoRepeatSubmit;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.Objects;
import java.util.Optional;

/**
 * @author guo
 */
@Getter
@Setter
@Table(name = "so_detail")
public class SoDetail {
    /**
     * 主键
     */
    @Id
    @TableId(type = IdType.AUTO)
    @Column(name = "so_detail_id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer soDetailId;

    /**
     * 单号
     */
    @Column(name = "bill_no")
    private String billNo;

    /**
     * 详情单号
     */
    @Column(name = "detail_no")
    private String detailNo;

    /**
     * 状态 0-新建
     */
    private Integer state;

    /**
     * 物料编码
     */
    @Column(name = "item_code")
    private String itemCode;

    /**
     * 物料名称
     */
    @Column(name = "item_name")
    private String itemName;

    /**
     * 物料规格
     */
    @Column(name = "spec")
    private String spec;

    /**
     * 物料型号
     */
    @Column(name = "model")
    private String model;

    /**
     * 物料名称
     */
    @Column(name = "img_url")
    private String imgUrl;

    /**
     * 包装详情ID
     */
    @Column(name = "pack_detail_id")
    private Integer packDetailId;

    /**
     * 包装描述
     */
    @Column(name = "pack_describe")
    private String packDescribe;


    private Double transRatio;
    /**
     * 订货数
     */
    @Column(name = "order_quantity")
    private Double orderQuantity;

    /**
     * 拣货时间
     */
    @TableField("pick_time")
    private LocalDateTime pickTime;

    public Double getOrderQuantity() {
        return ObjectUtil.isEmpty(orderQuantity) ? 0.00 : orderQuantity;

    }

    /**
     * 预分配数
     */
    @Column(name = "expect_allotted_quantity")
    private Double expectAllottedQuantity;

    /**
     * 分配数
     */
    @Column(name = "allotted_quantity")
    private Double allottedQuantity;

    public Double getAllottedQuantity() {
        return ObjectUtil.isEmpty(allottedQuantity) ? 0.00 : allottedQuantity;

    }

    private Integer allotPackDetailId;
    private String allotPackDescribe;
    private Double allotTransRatio;
    /**
     * 拣货数量
     */
    @Column(name = "pick_quantity")
    private Double pickQuantity;

    private Integer pickPackDetailId;
    private String pickPackDescribe;
    private Double pickTransRatio;
    /**
     * 发货数量
     */
    @Column(name = "delivery_quantity")
    private Double deliveryQuantity;

    public Double getDeliveryQuantity() {
        return ObjectUtil.isEmpty(deliveryQuantity) ? 0.00 : deliveryQuantity;
    }

    private Double checkQuantity;

    /**
     * 退料数量
     */
    private Double returnQty;

    public Double getReturnQty() {
        return ObjectUtil.isEmpty(returnQty) ? 0.00 : returnQty;
    }


    private Integer deliveryPackDetailId;
    private String deliveryPackDescribe;
    private Double deliveryTransRatio;
    /**
     * 自定义1
     */
    @Column(name = "so_udf_ds1")
    private String soUdfDs1;

    /**
     * 自定义2
     */
    @Column(name = "so_udf_ds2")
    private String soUdfDs2;

    /**
     * 自定义3
     */
    @Column(name = "so_udf_ds3")
    private String soUdfDs3;

    /**
     * 备注
     */
    private String memo;


    @TableField(exist = false)
    private String remake;
    /**
     * ERP源单id
     */
    @Column(name = "erp_bill_id")
    private Integer erpBillId;

    public String getItemCodeAndDeptId() {
        return String.format("%s-%s-%s-", this.itemCode, this.rob,this.deptId);
//       return this.itemCode + this.rob;
    }

    private String mergeNo;
    private Double weight;
    private Double size;
    private Integer bomSplit;
    private String allotDetailNo;
    @TableField(exist = false)
    private Double number;
    @TableField(exist = false)
    @ApiModelProperty("库存余量")
    private Double marginQty;

    /**
     * 波次明细ID
     */
    private Integer waveDetailId;
    @TableField(exist = false)
    @ApiModelProperty("基本单位原始数量")
    private Double originQty;

    @ApiModelProperty("基本待检验数量")
    @TableField(exist = false)
    private Double qcQty;
    /**
     * 目标红蓝字
     */
    @Column(name = "rob")
    private Boolean rob;

    @TableField(exist = false)
    private String orderBillNo;


    //合计库存数量
    @TableField(exist = false)
    private Double totalNum;

    //可出库数量
    @TableField(exist = false)
    private Double subQty;
    //可出库数量
    @TableField(exist = false)
    private String soMasterCode;

    private Integer sort;

    @TableField(exist = false)
    private Double allReturnQuantity;

    public Double getAllReturnQuantity() {
        return Optional.ofNullable(allReturnQuantity).orElse(0.00);
    }

    @TableField(exist = false)
    private Double newReturnQty;
    private Integer pickUserId;
    private String pickUserName;

    @TableField(exist = false)
    private Integer inventoryId;
    @TableField(exist = false)
    private String cellCodes;
    @TableField(exist = false)
    private String feedCategory;

    private Integer stockId;
    //备注
    private String remark;

    @TableField(exist = false)
    private String adItemName;
    @TableField(exist = false)
    private Integer deptId;

    /**
     * 合计退料数量
     */
    @TableField("total_return_qty")
    private Double totalReturnQty;

    public Double getTotalReturnQty() {
        return ObjectUtil.isNotEmpty(totalReturnQty) ? totalReturnQty : 0.00;
    }

    public String itemCodeAndMergeNo(String tradeNo) {
        return itemCode + tradeNo;
    }

    public Integer getSort() {
        return ObjectUtil.isNotEmpty(sort) ? sort : 0;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        SoDetail soDetail = (SoDetail) o;
        if (ObjectUtil.isEmpty(erpBillId)) {
            return  false   ;
        }
        ;
        return   erpBillId.equals(soDetail.erpBillId) && bomSplit.equals(soDetail.bomSplit)&&itemCode.equals(soDetail.itemCode);
    }

    @Override
    public int hashCode() {
        return Objects.hash( erpBillId, bomSplit,itemCode);
    }
}